package net.celisdelafuente.java.Acacia.pages;

import java.sql.SQLException;

//import cfe.java.Acacia.entity.*;
/*
import cfe.java.Acacia.entity.Basket;
import cfe.java.Acacia.entity.Item;
import cfe.java.Acacia.entity.ItemsSale;
import cfe.java.Acacia.entity.Sale;
import cfe.java.Acacia.entity.Stock;
*/

import net.celisdelafuente.java.Acacia.entity.*;
import net.celisdelafuente.java.Acacia.util.Environment;

import org.apache.click.Page;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class BasePage extends Page {
	
	private Environment environment = new Environment(); 
	
	private final String DATABASE_URL = "jdbc:derby:" + 
			System.getProperty("user.home") + 
			"/acacia/data/acaciadb;create=true;user=APP;";
	
	protected Dao<Author, Integer> author;
	protected Dao<Book, Integer> book;
	protected Dao<BookClass, Integer> bookclassDao;
	protected Dao<BooksInventory, Integer> booksinventoryDao;
	protected Dao<Classification, Integer> classification;
	protected Dao<Publisher, Integer> publisher;
	protected Dao<Supplier, Integer> supplier;
	protected String isbnkey = "";
	
	public BasePage() {
		
	}
	
	public String getIsbnkey() {
		return isbnkey;
	}

	public void setIsbnkey(String isbnkey) {
		this.isbnkey = isbnkey;
	}

	public ConnectionSource GetConnection() throws SQLException {
		JdbcConnectionSource connectionSource = 
			new JdbcConnectionSource(DATABASE_URL);
		try {
			this.setupDatabase(connectionSource);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connectionSource;
	}
	
	public void CloseConnection(ConnectionSource connection) 
	throws SQLException {
		connection.close();
	}
	
	public void CreateSchema(ConnectionSource connectionSource) 
			throws Exception {
		//this.setupDatabase(connectionSource);
		// if you need to create the table
		TableUtils.createTable(connectionSource, Author.class);
		TableUtils.createTable(connectionSource, Book.class);
		TableUtils.createTable(connectionSource, BookClass.class);
		TableUtils.createTable(connectionSource, BooksInventory.class);
		TableUtils.createTable(connectionSource, Classification.class);
		TableUtils.createTable(connectionSource, Supplier.class);
		TableUtils.createTable(connectionSource, Publisher.class);
	}
	
	public void DropSchema(ConnectionSource connectionSource) 
			throws SQLException {
		TableUtils.dropTable(connectionSource,Author.class,true);
		TableUtils.dropTable(connectionSource,Book.class,true);
		TableUtils.dropTable(connectionSource,BookClass.class,true);
		TableUtils.dropTable(connectionSource,BooksInventory.class,true);
		TableUtils.dropTable(connectionSource,Classification.class,true);
		TableUtils.dropTable(connectionSource,Publisher.class,true);
		TableUtils.dropTable(connectionSource,Supplier.class,true);		
	}
	
	private void setupDatabase(ConnectionSource connectionSource) 
	throws Exception {
		author = DaoManager.createDao(connectionSource, Author.class);
		book = DaoManager.createDao(connectionSource, Book.class);
		bookclassDao = DaoManager.createDao(connectionSource, 
				BookClass.class);
		booksinventoryDao = DaoManager.createDao(connectionSource, 
				BooksInventory.class);
		classification = DaoManager.createDao(connectionSource, 
				Classification.class);
		supplier = DaoManager.createDao(connectionSource, Supplier.class);
		publisher = DaoManager.createDao(connectionSource, 
				Publisher.class);
	}

	@Override
	public void onInit() {
		// TODO Auto-generated method stub
		super.onInit();
		
		environment.configure();
	}
}
